<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="76px">
        <el-form-item label="处理状态">
          <el-select
            v-model="searchParam.progressStatus"
            placeholder="请选择处理状态"
            clearable
          >
            <el-option
              v-for="item in tabList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="工单状态">
          <el-select
            v-model="searchParam.repairStatus"
            placeholder="请选择工单状态"
            clearable
          >
            <el-option
              v-for="item in useDictionary('EamRepairStatus')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="故障状态">
          <el-select
            style="width: 100%"
            v-model="searchParam.faultStatus"
            placeholder="请选择故障状态"
            clearable
          >
            <el-option
              v-for="item in useDictionary('EamFaultStatus')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="故障等级">
          <el-select
            style="width: 100%"
            v-model="searchParam.repairLevel"
            placeholder="请选择故障等级"
            clearable
          >
            <el-option
              v-for="item in useDictionary('EamFaultLevel')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="故障来源">
          <el-select
            style="width: 100%"
            v-model="searchParam.repairSource"
            placeholder="请选择故障来源"
            clearable
          >
            <el-option
              v-for="item in useDictionary('EamFaultSource')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="模具信息">
          <el-input
            v-model="searchParam.assetInfo"
            placeholder="请输入模具编号或名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="报修时间">
          <el-date-picker
            v-model="searchParam.repairTimeSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="getrepairTime"
          />
        </el-form-item>
        <!-- <el-form-item label="报修人">
          <el-input
            v-model="searchParam.repairUser"
            placeholder="请输入报修人"
            clearable
            @keyup.enter="search"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="search">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="reset">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()">
          <i-ep-plus />
          新增
        </el-button>
        <el-button
          @click="
            useDownload(
              '/andon/andcall/callInformExport',
              '故障报修工单',
              searchParam
            )
          "
        >
          <template #icon><i-ep-download /></template>导出</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        @sort-change="onTableSortChanged"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="报修单编号"
          prop="repairCode"
          min-width="180"
          show-overflow-tooltip
          sortable
        />
        <el-table-column
          label="工单状态"
          prop="repairStatus"
          min-width="120"
          sortable
          align="center"
        >
          <template #default="scope">
            <el-tag type="warning" v-if="scope.row.repairStatus == 0">
              {{ useDictionary("EamRepairStatus", scope.row.repairStatus) }}
            </el-tag>
            <el-tag
              v-if="
                scope.row.repairStatus * 1 > 0 && scope.row.repairStatus * 1 < 4
              "
            >
              {{ useDictionary("EamRepairStatus", scope.row.repairStatus) }}
            </el-tag>
            <el-tag v-if="scope.row.repairStatus * 1 >= 4" type="info">
              {{ useDictionary("EamRepairStatus", scope.row.repairStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="报修时间"
          prop="repairTime"
          min-width="180"
          show-overflow-tooltip
          sortable
          align="center"
        />
        <el-table-column
          label="报修人"
          prop="repairUserName"
          min-width="100"
          show-overflow-tooltip
          sortable
          align="center"
        />
        <el-table-column
          label="故障状态"
          prop="faultStatus"
          min-width="120"
          sortable
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("EamFaultStatus", scope.row.faultStatus) }}
          </template>
        </el-table-column>
        <el-table-column
          label="模具名称"
          prop="assetName"
          min-width="200"
          show-overflow-tooltip
          sortable
        />
        <el-table-column
          label="模具编号"
          prop="assetCode"
          min-width="150"
          show-overflow-tooltip
          sortable
        />
        <el-table-column
          label="故障描述"
          prop="remarks"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column
          label="故障等级"
          prop="repairLevel"
          min-width="100"
          sortable
          align="center"
        >
          <template #default="scope">
            <el-tag
              type="danger"
              v-if="
                scope.row.repairLevel == '1' && scope.row.repairStatus * 1 < 4
              "
            >
              {{ useDictionary("EamFaultLevel", scope.row.repairLevel) }}
            </el-tag>
            <el-tag type="info" v-if="scope.row.repairStatus * 1 >= 4">
              {{ useDictionary("EamFaultLevel", scope.row.repairLevel) }}
            </el-tag>
            <el-tag
              v-if="
                scope.row.repairLevel !== '1' && scope.row.repairStatus * 1 < 4
              "
            >
              {{ useDictionary("EamFaultLevel", scope.row.repairLevel) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="故障来源"
          prop="repairSource"
          min-width="100"
          sortable
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("EamFaultSource", scope.row.repairSource) }}
          </template>
        </el-table-column>
        <el-table-column
          label="使用部门"
          prop="usingDepartmentName"
          min-width="160"
          show-overflow-tooltip
          sortable
          align="center"
        />
        <el-table-column
          label="维修完成时间"
          prop="repairEndTime"
          min-width="160"
          show-overflow-tooltip
          sortable
          align="center"
        />
        <el-table-column
          label="维修班组"
          prop="repairTeamName"
          min-width="150"
          show-overflow-tooltip
          sortable
          align="center"
        />
        <el-table-column
          label="维修情况"
          prop="repairSituation"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column fixed="right" label="操作" width="170" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
            >
              <i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              :disabled="scope.row.repairStatus != 0"
              @click="openDialog(scope.row)"
            >
              <i-ep-edit />
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mold-eam-repair-order-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :assetKind="assetKind"
      @reset-table="(id:string) => (id?.length ? getTableList() : search())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { filterOrder } from "@/utils/commonHooks";
defineOptions({
  name: "EAMMOLDEAMREPAIRORDERS",
  inheritAttrs: false,
});

const tabList = [
  { name: "所有", id: 0 },
  { name: "待处理", id: 1 },
  { name: "处理中", id: 2 },
  { name: "已完成", id: 3 },
];

const assetKind = 3;
// 请求参数
const requestApi = "/eam/mouldrepairorder/queryMyReportOrder";

// 是否需要分页
const pagination = true;

const initParam = reactive({
  sort: {
    column: "",
    orderBy: "",
  },
});

// 初始化搜索参数数据
const searchParamInit = {
  repairStatus: "",
  repairUser: "",
  faultStatus: "",
  repairLevel: "",
  repairSource: "",
  assetInfo: "",
  repairTimeSearch: [],
  repairTimeStart: "",
  repairTimeEnd: "",
  assetKind,
};

const deleteParam = {
  api: "/eam/mouldrepairorder/delete",
  data: { assetKind },
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);
// 排序
// 监听表格排序方法
function onTableSortChanged(column: any) {
  const { prop, order } = column;
  console.log(prop, order);

  initParam.sort = {
    column: prop,
    orderBy: filterOrder(order),
  };
}
// 监听页面 initParam 改化，重新获取表格数据
watch(() => initParam, getTableList, { deep: true });
onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/eam/mouldrepairorder/get/${id}`,
    method: "get",
    data: {
      assetKind,
    },
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开维修单表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改维修单";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增维修单";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开维修单表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "维修单详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getrepairTime(data: any) {
  if (data) {
    searchParam.value.repairTimeStart = data[0];
    searchParam.value.repairTimeEnd = data[1];
  } else {
    searchParam.value.repairTimeStart = "";
    searchParam.value.repairTimeEnd = "";
  }
}
</script>
